package com.coding.service.impl;

import com.coding.dto.RestBean;
import com.coding.service.LikeService;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author Cynicism
 * @version 1.0
 * @project DyApp
 * @description
 * @date 2024/7/9 23:37:12
 */
@Service
@Slf4j
public class LikeServiceImpl implements LikeService {
    @Resource
    private RocketMQTemplate rocketMQTemplate;


    @Override
    public RestBean<String> like(String liveStreamId) {
        // 异步发送点赞事件到RocketMQ
        rocketMQTemplate.asyncSend("like-topic", MessageBuilder.withPayload(liveStreamId).build(), new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                log.info("点赞事件发送成功: {}", liveStreamId);
            }

            @Override
            public void onException(Throwable throwable) {
                log.error("点赞事件发送失败: {}", liveStreamId, throwable);
            }
        });


        return RestBean.success("点赞成功");
    }



}
